Walkthrough 11-9: Look up data by calling a flow
In this walkthrough, you continue to work with the transformation in getMultipleFlights. You will:
ยท Call a flow from a DataWeave expression.
Starting file
If you did not complete the previous walkthrough, you can get a starting file here. This file is also located in the solutions folder of the student files ZIP located in the Course Resources.
Create a lookup flow
1. Return to the Transform Message properties view for the transformation in postMultipleFlights.
2. Copy the getNumSeats function.
3. Drag a Transform Message component from the Mule Palette and drop it at the bottom of the canvas to create a new flow.
4. Change the name of the flow to getTotalSeats.
5. In the Transform Message properties view of the component in getTotalSeats, paste the function you copied into the script header.
6. In the script body, call the function, passing to it the payload's planeType property.
getNumSeats(payload.planeType)
Call a flow from a DataWeave expression
7. Return to the Transform Message properties view for the transformation in postMultipleFlights.
8. Add a property called totalSeats inside the expression (keep the other one commented out).
9. Change the return type of the map function from Flight to Object.
10. Set totalSeats equal to the return value from the DataWeave lookup() function.
totalSeats: lookup()
11. Pass to lookup() an argument that is equal to the name of the flow to call: "getTotalSeats".
12. Pass an object to lookup() as the second argument.
13. Give the object a field called planeType (to match what the flow is expecting) and set it equal to the value of the planeType field.
totalSeats: lookup("getTotalSeats",{planeType: object.planeType})
14. Look at the preview.
15. Look at the warning you get.
Test the application
16. Save the file and, if necessary, run or debug the project.
17. In Advanced REST Client, make sure the method is set to POST and the request URL is set to http://localhost:8081/multipleflights.
18. Set a Content-Type header to application/xml.
19. Set the request body to the value contained in the flights-example.xml file in the src/test/resources folder.
20. Send the request; you should get the DataWeave representation of the return flight data and each flight should have a totalSeats property equal to 150 or 300.
21. Return to Anypoint Studio.
22. Stop the project.
23. Close the project.